package com.datn.DataAccess;

import java.io.IOException;
import java.util.ArrayList;

import com.datn.utils.FileUtils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class UserDatabaseAdapter {

	public static final String TAG ="UserDatabaseAdapter";
	
	public static final String KEY_USERNAME = "Username";
	public static final String KEY_PASSWORD = "Password";
	public static final String KEY_EMAIL = "Email";
	public static final String KEY_ROLE = "Role";
	
	
	
	private static final String DATABASE_NAME = "NearLocationDatabase";
	private static final String DATABASE_TABLE = "tblAccount";
	private static final int DATABASE_VERSION = 6;
	
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDB;
	
	private final Context mContext;
	
	
	
	public UserDatabaseAdapter(Context ctx){
		this.mContext = ctx;
	}
	
	public UserDatabaseAdapter open()
	{		
		mDbHelper = new DatabaseHelper(mContext, DATABASE_NAME, null, DATABASE_VERSION);
		mDB = mDbHelper.getWritableDatabase();
		return this;
	}
	
	public void close(){
		mDbHelper.close();
	}
	
	public Cursor getAllUsers(){
		return mDB.query(DATABASE_TABLE, new String[] {KEY_USERNAME, KEY_PASSWORD, KEY_EMAIL, KEY_ROLE}, null, null, null, null, null);
	}
	
	
	public long createUser(String name, String password, String email){
		ContentValues inititalValues = new ContentValues();
			inititalValues.put(KEY_USERNAME, name);
			inititalValues.put(KEY_PASSWORD, password);
			inititalValues.put(KEY_EMAIL, email);
			inititalValues.put(KEY_ROLE, 1);
		return mDB.insert(DATABASE_TABLE, null, inititalValues);
	}
	
	public boolean isExistUser(String username){
		if ((username==null)||(username.trim().isEmpty())) return false;
		Cursor cursor =  mDB.query(DATABASE_TABLE, new String[]{KEY_USERNAME}, KEY_USERNAME+"=?", new String[]{username}, null, null, null);
		return (cursor.getCount()>0);
	}
	
	public boolean isExistEmail(String stEmail){
		if ((stEmail==null)||(stEmail.trim().isEmpty())) return false;
		Cursor cursor =  mDB.query(DATABASE_TABLE, new String[]{KEY_EMAIL}, KEY_EMAIL+"=?", new String[]{stEmail}, null, null, null);
		return (cursor.getCount()>0);
	}
	
	public boolean isUser(String username, String password){
		if ((username==null)||(password==null)) return false;
		if ((username.equalsIgnoreCase(""))||(password.equalsIgnoreCase(""))) return false;
		Cursor cursor =  mDB.query(DATABASE_TABLE, new String[]{KEY_USERNAME, KEY_PASSWORD}, KEY_USERNAME+"=? AND "+KEY_PASSWORD+"=?", new String[]{username,password}, null, null, null);
		return (cursor.getCount()>0);
	}
	
	
	
	
	/*public long createUser(String name){
		ContentValues inititalValues = new ContentValues();
		inititalValues.put(KEY_USERNAME, name);
		return mDB.insert(DATABASE_TABLE, null, inititalValues);
	}
	
	public boolean deleteUser(long rowId)
	{
		return mDB.delete(DATABASE_TABLE, KEY_ID + "=" + rowId, null) >0;
	}
	public Cursor getAllUsers(){
		return mDB.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME}, null, null, null, null, null);
	}*/
}
